package com.guigu.community.controller;


import com.github.pagehelper.PageInfo;
import com.guigu.community.pojo.Carousel;
import com.guigu.community.service.CarouselService;
import com.guigu.community.vo.CarouselVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/carousel")
@CrossOrigin
public class CarouselController {
    @Autowired
    private CarouselService carouselService;

    @PostMapping("/AllCarousel/{current}/{pageSize}")
    public PageInfo<Carousel> AllCarousel(@PathVariable Integer current,
                                          @PathVariable Integer pageSize,
                                          @RequestBody CarouselVo vo){
        return carouselService.queryVo(current, pageSize, vo);
    }

//    不带分页查询
    @PostMapping("AllCarousel2")
    public List<Carousel> AllCarousel2(@RequestBody CarouselVo vo){
        vo.setSearchActive(String.valueOf(1));
        return carouselService.queryCarousel(vo);
    }


    @PostMapping("add")
    public String add(@RequestBody Carousel carousel){
        boolean save = carouselService.save(carousel);
        if (save)
            return "yes";
        return "no";
    }

    @PostMapping("CarouselUpdate")
    public String CarouselUpdate(@RequestBody Carousel carousel){
        boolean b = carouselService.updateById(carousel);
        if (b)
            return "yes";
        return "no";
    }

    @DeleteMapping("deleteCarousel/{cid}")
    public String deleteCarousel(@PathVariable Integer cid){
        boolean b = carouselService.removeById(cid);
        if (b)
            return "yes";
        return "no";
    }

    //根据ID修改轮播图的状态
    @PostMapping("updateActive/{cid}/{activeId}")
    public String updateActive(@PathVariable Integer cid ,
                               @PathVariable Integer activeId){
        Boolean aBoolean = carouselService.modifCarouselActive(cid, activeId);
        if (aBoolean)
            return "yes";
        return "no";
    }
}
